<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Activity extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Activity_model');
	}

	public function index()
	{
		$userid = $this->thatUser['id'];
		$page = _get_page();
		$pagesize = 10;
		$arrParam = array();
		$arrWhere = array(
			'userid' => $userid,
			'status' => 1
		); // 条件
		
		$list = $this->Activity_model->fetch_page($page, $pagesize, $arrWhere);
		
		// 处理展示的字段
		if ( ! empty($list['rows']) )
		{
			foreach ($list['rows'] as $key => $row)
			{
				$list['rows'][$key]['w_time'] = is_numeric($row['begtime']) ? (date('Y-m-d', (int) $row['begtime'])) : $row['begtime'];
				$list['rows'][$key]['endtime'] = date('Y-m-d', $row['inendtime']);
				$list['rows'][$key]['title'] = mb_substr($row['title'], 0, 24);
			}
		}
		
		// 分页
		$pagecfg = array();
		$pagecfg['base_url'] = _create_url('m/activity', $arrParam);
		$pagecfg['total_rows'] = $list['count'];
		$pagecfg['cur_page'] = $page;
		$pagecfg['per_page'] = $pagesize;
		// $this->load->library('pagination');
		$this->pagination->initialize($pagecfg);
		$list['pages'] = $this->pagination->create_links();
		
		$result = array(
			'list' => $list,
			'oSysAct' => _get_config('activity'),
		);
		
		// print_r($list);
		$this->load->view('m/activity', $result);
	}

	/* 增加活动 -- 已废弃 */
	public function add()
	{
		// 需要修改
		$id = _get_key_val($this->input->get('id'), TRUE);
		$result = array();
		
		if ( $this->input->is_post() )
		{
			// 验证规则
			$config = array(
				array(
					'field' => 'type',
					'label' => '通告性质',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'begtime',
					'label' => '工作时间',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'place',
					'label' => '工作地点',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'summary',
					'label' => '工作内容',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'actnum',
					'label' => '名额',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'workfee',
					'label' => '工作费用',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'inendtime',
					'label' => '报名截止时间',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'img',
					'label' => '通告封面',
					'rules' => 'trim|required'
				)
			);
			
			$this->form_validation->set_rules($config);
			
			if ( $this->form_validation->run() === TRUE )
			{
				
				$data = array(
					'title' => $this->input->post('title'),
					'type' => $this->input->post('type'),
					'begtime' => strtotime($this->input->post('begtime')),
					'endtime' => strtotime($this->input->post('endtime')),
					'place' => $this->input->post('place'),
					'summary' => $this->input->post('summary'),
					'actnum' => (int) $this->input->post('actnum'),
					'workfee' => $this->input->post('workfee'),
					'inendtime' => strtotime($this->input->post('inendtime')),
					'img' => $this->input->post('img'),
					'status' => 1,
					'op_userid' => $this->loginID,
					'op_username' => $this->loginUserName,
					'op_time' => time()
				);
				
				$id = _get_key_val($this->input->get('id'), TRUE);
				if ( $id )
					$data['id'] = $id;
				
				if ( ! $id )
				{
					$data['userid'] = $this->thatUser['id'];
					$data['nickname'] = $this->thatUser['nickname'];
				}
				$this->Activity_model->insert($data);
				redirect('/m/activity');
				exit();
			}
		} // -is_post()
		
		if ( ! empty($id) )
		{
			
			$info = $this->Activity_model->get_info_by_id($id);
			$result = array(
				'info' => $info
			);
		}
		$oSysActType = _get_config('activity');
		$result['oSysActType'] = $oSysActType;
		
		$this->load->view('m/activityadd', $result);
	}

	/*
	 * public function add()
	 * {
	 * $this->load->view('m/activityadd');
	 * }
	 */
	public function getinfo()
	{
		$res = array(
			'code' => 0,
			'data' => array()
		);
		$id = _get_key_val($this->input->get('id'), TRUE);
		$o = $this->Activity_model->get_info_by_id($id, 'linkman,contact,memo');
		$res['code'] = 200;
		$res['data'] = $o;
		
		$this->view->json($res);
		exit();
	}

	public function del()
	{
		$res = array(
			'code' => 0,
			'data' => array()
		);
		$id = _get_key_val($this->input->post('id'), TRUE);
		
		$this->Activity_model->update_by_where(array(
			'id' => $id,
			'userid' => $this->thatUser['id']
		), array(
			'status' => 0
		));
		$res['code'] = 200;
		
		$this->view->json($res);
	}

	public function enterlist()
	{
		$aid = (int) $this->input->get('aid');
		
		$page = _get_page();
		$pagesize = 120;
		$arrParam = array(
			'aid' => $aid
		);
		$arrWhere = array(
			'actid' => $aid
		);
		
		$o = $this->Activity_model->get_by_id($aid);
		
		$dbprefix = $this->Activity_model->db->dbprefix;
		$this->load->model('Activityenter_model');
		$tb = $dbprefix . 'activity_enter a left join ' . $dbprefix . 'user b on(a.userid=b.id and b.status=1)';
		$list = $this->Activityenter_model->fetch_page($page, $pagesize, $arrWhere, 'a.userid,b.nickname,b.userlogo', 'a.addtime desc', $tb);
		// echo $this->db->last_query();die;
		
		// 分页
		$pagecfg = array();
		$pagecfg['base_url'] = _create_url('act/enterlist', $arrParam);
		$pagecfg['total_rows'] = $list['count'];
		$pagecfg['cur_page'] = $page;
		$pagecfg['per_page'] = $pagesize;
		// $this->load->library('pagination');
		$this->pagination->initialize($pagecfg);
		$list['pages'] = $this->pagination->create_links();
		
		$result = array(
			'list' => $list,
			'o' => $o
		);
		
		$this->load->view('m/actenterlist', $result);
	}

	public function noticeAdd()
	{
		$id = _get_key_val($this->input->get('id'), TRUE);
		$result = array();
		
		// 没有有提交
		if ( $this->input->is_post() )
		{
			// 设置规则
			$config = array(
				array(
					'field' => 'title',
					'lable' => '通告名称',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'type',
					'label' => '通告性质',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'endtime',
					'label' => '报名截止时间',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'img',
					'label' => '通告封面',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'cost',
					'label' => '费用预算',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'category',
					'label' => '工作性质',
					'rules' => 'trim|required'
				)
			);
			// 工作
			$workConfig = array(
				array(
					'field' => 'begtime',
					'label' => '工作时间',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'place',
					'label' => '工作地点',
					'rules' => 'trim|required'
				),
// 				array(
// 					'field' => 'sex',
// 					'label' => '性别',
// 					'rules' => 'trim|required'
// 				),
// 				array(
// 					'field' => 'people_num',
// 					'label' => '人数',
// 					'rules' => 'trim|required'
// 				),
// 				array(
// 					'field' => 'height',
// 					'label' => '身高',
// 					'rules' => 'trim|required'
// 				),
// 				array(
// 					'field' => 'style',
// 					'label' => '风格',
// 					'rules' => 'trim|required'
// 				)
			);
			// 面试
			$interviewConfig = array(
				array(
					'field' => 'address',
					'label' => '面试地点',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'interview_begin_time',
					'label' => '面试时间',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'summary',
					'label' => '模特要求',
					'rules' => 'trim|required'
				),
// 				array(
// 					'field' => 'sex',
// 					'label' => '性别',
// 					'rules' => 'trim|required'
// 				),
// 				array(
// 					'field' => 'people_num',
// 					'label' => '人数',
// 					'rules' => 'trim|required'
// 				),
// 				array(
// 					'field' => 'height',
// 					'label' => '身高',
// 					'rules' => 'trim|required'
// 				),
// 				array(
// 					'field' => 'style',
// 					'label' => '风格',
// 					'rules' => 'trim|required'
// 				)
			);
			// 制作通告
			$gameConfig = array(
				array(
					'field' => 'begtime',
					'label' => '工作开始时间',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'place',
					'label' => '工作地点',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'm_content',
					'label' => '制作要求',
					'rules' => 'trim|required'
				),
				array(
					'field' => 'm_num',
					'label' => '人数',
					'rules' => 'trim|required'
				)
			);
			
			$this->form_validation->set_rules($config);
			if ( $this->form_validation->run() === TRUE )
			{
				$data = array(
					'title' => $this->input->post('title'),
					'type' => $this->input->post('type'),
					'inendtime' => strtotime($this->input->post('endtime')),
					'workfee' => $this->input->post('cost'),
					'img' => $this->input->post('img'),
					'status' => 1,
					'op_userid' => $this->loginID,
					'op_username' => $this->loginUserName,
					'op_time' => time(),
					'content' => $this->input->post('introduction'),
					'other' => $this->input->post('other')
				);
				switch ($data['type'])
				{
					case '3':
						// 制作通告
						$this->form_validation->set_rules($gameConfig);
						if ( $this->form_validation->run() === TRUE )
						{
							$data['category'] = $this->input->post('category');
							$data['summary'] = $this->input->post('m_content');
							$data['place'] = $this->input->post('place');
							$data['begtime'] = strtotime($this->input->post('begtime'));
							$data['pro_num'] = intval($this->input->post('m_num'));
						}
						break;
					case '2':
						// 工作
						$this->form_validation->set_rules($workConfig);
						if ( $this->form_validation->run() === TRUE )
						{
							// 要求
// 							$data['summary'] = $this->input->post('sex');
// 							$data['summary'] .= '||' . $this->input->post('people_num');
// 							$data['summary'] .= '||' . $this->input->post('height');
// 							$data['summary'] .= '||' . $this->input->post('style');
							$data['summary'] = $this->input->post('summary');
							
							$data['category'] = $this->input->post('category');
							$data['place'] = $this->input->post('place');
							$data['begtime'] = strtotime($this->input->post('begtime'));
							$data['endtime'] = strtotime($this->input->post('endtime'));
							$data['content'] = $this->input->post('introduction');
						}
						break;
					case '1':
						// 面试
						$this->form_validation->set_rules($interviewConfig);
						if ( $this->form_validation->run() === TRUE )
						{
							// 要求
// 							$data['summary'] = $this->input->post('sex');
// 							$data['summary'] .= '||' . $this->input->post('people_num');
// 							$data['summary'] .= '||' . $this->input->post('height');
// 							$data['summary'] .= '||' . $this->input->post('style');
							$data['summary'] = $this->input->post('summary');
							
							$data['category'] = $this->input->post('category');
							$data['place'] = $this->input->post('address');
							$data['begtime'] = strtotime($this->input->post('interview_begin_time'));
							$data['content'] = $this->input->post('introduction');
						}
						break;
					default:
						break;
				}
				
				$id = _get_key_val($this->input->get('id'), TRUE);
				if ( $id )
				{
					$data['id'] = $id;
					// 更新
					$this->Activity_model->update_by_id($id, $data);
// 					echo $this->Activity_model->db->last_query();die;
				} else
				{
					
					$data['userid'] = $this->thatUser['id'];
					$data['nickname'] = $this->thatUser['nickname'];
					$data['addtime'] = time();
					// 新增
					$this->Activity_model->insert($data);
				}
				
				redirect('/m/activity');
				exit();
			}
		}
		// 初始化模特要求的数据
// 		$sex = '';
// 		$peopleNum = '';
// 		$height = '';
// 		$style = '';
		
		if ( ! empty($id) )
		{
			$info = $this->Activity_model->get_info_by_id($id);
// 			if ( ! empty($info) && ((1 == $info['type']) || (2 == $info['type'])) && (strstr($info['summary'], '||')) )
// 			{
// 				// 处理要求数据
// 				$summaryArr = explode('||', $info['summary']);
// 				$sex = $summaryArr[0];
// 				$peopleNum = $summaryArr[1];
// 				$height = $summaryArr[2];
// 				$style = $summaryArr[3];
// 			}
			
			// 处理时间
// 			if ( ! empty($info) && (is_numeric($info['begtime'])) )
// 			{
// 				$info['begtime'] = date('Y-m-d H:i:s', (int) $info['begtime']);
// 			}
			
			$result = array(
				'info' => $info,
// 				'sex' => $sex,
// 				'people_num' => $peopleNum,
// 				'height' => $height,
// 				'style' => $style,
// 				'noticeArray' => _get_config('work_cat')
			);
		}
		
		// 配置
		$oSysActType = _get_config('activity');
		$result['oSysActType'] = $oSysActType;
		// 通告性质
		$noticeArr = _get_config('work_cat');
		$result['noticeArr'] = $noticeArr;
		$this->load->view('m/noticeadd', $result);
	}
	
	//删除通告
	public function noticedelete()
	{
		$id = $this->input->get('id');
		if (empty($id))
		{
			alert('删除错误',base_url().'m/activity');
		}
		$actid = _get_key_val($this->input->get('id'),true);
		$act = $this->Activity_model->get_info_by_id($actid);
		if ($act['userid'] != $this->thatUser['id'])
		{
			alert('没有权限操作',base_url().'m/activity');
		}
		$this->Activity_model->update_by_id($actid,array('status'=>-1));
		redirect('/m/activity');
	}
}